//
//  RootViewController.m
//  MyOWNUITableView
//
//  Created by Jordy Heemskerk on 14-04-10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "RootViewController.h"
#import "OfferDetails.h"
#import "Product.h"
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>


@implementation RootViewController


- (void)viewDidLoad {
    [super viewDidLoad];
	
	/*MYSQL *mysql;
	
	mysql = mysql_init(NULL);
	if (mysql == NULL) {
		printf("Error %u: %s\n", mysql_errno(mysql), mysql_error(mysql));
		exit(1);
	}
	
	
	if (mysql_real_connect(mysql, "192.168.1.31", "root", "toor", "shopdeluxe", 0, NULL, 0) == NULL) {
		printf("Error %u: %s\n", mysql_errno(mysql), mysql_error(mysql));
		exit(1);
	}
	
	mysql_query(mysql, "insert into products (name) values ('Autos')");*/
	
	[self setTitle:@"Offers"];
								 
    products = [[NSMutableArray alloc] init];

	[products addObject:[[Product alloc] initWithName:@"Getekend Fietsje" Description:@"Mooie oma fiets!\nHelemaal nieuw, speciaal voor JOU getekent! Moet toch niet veel gekker worden.." Price:(double)99.94 OldPrice:(double)100 Image:[UIImage imageNamed:@"fiets.gif"] AndShop:@"Junkie"]];
	[products addObject:[[Product alloc] initWithName:@"Gouden Tand" Description:@"Voor de ECHTE gangsters onder ons. Nu halen, METEEN betalen!" Price:(double)499.99 OldPrice:(double)450 Image:[UIImage imageNamed:@"goudentand.gif"] AndShop:@"PipaShop"]];
	
	//[self setTitle:[NSString stringWithFormat:@"\u20AC %1.2f",[[[products objectAtIndex:0] price] doubleValue]]];

	
}

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release anything that can be recreated in viewDidLoad or on demand.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [products count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	Product *p = [products objectAtIndex:indexPath.row];
	
	cell.textLabel.text = [p name];
	[[cell imageView] setImage: [p image]];
	
	[p release];

    return cell;
}




// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    OfferDetails *details = [[OfferDetails alloc] initWithNibName:@"OfferDetails" bundle:[NSBundle mainBundle]];
	Product *p = [products objectAtIndex:indexPath.row];
	details.product = p;
	[self.navigationController pushViewController:details animated:YES];
	[details release];
	details = nil;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
	[products release];
    [super dealloc];
}


@end

